package com.hex.ds.hdtp.core.app.data.transfer.handler.impl;

import com.hex.ds.hdtp.core.app.data.contrast.service.IContrastTableStructureService;
import com.hex.ds.hdtp.core.app.data.transfer.converter.IDataTransferLogConverter;
import com.hex.ds.hdtp.core.app.data.transfer.converter.IDataTransferSqlStructureConverter;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferListService;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferLogService;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferTaskService;
import com.hex.ds.hdtp.core.app.metadata.service.ISourceConfInfoService;
import com.hex.ds.hdtp.core.app.metadata.service.ISourceInfoService;
import com.hex.ds.hdtp.core.inf.common.utils.JdbcUtil;
import com.hex.ds.hdtp.core.inf.data.contrast.service.impl.DataInfoSelectHandlerContext;
import com.hex.ds.hdtp.core.inf.metadata.po.SourceConfInfoPo;
import com.hex.ds.hdtp.core.inf.metadata.repository.impl.SourceConfInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("MYSQL_GAUSS_JDBC")
public class MysqlGaussTransferHandlerJdbcService extends AbstractDataTransferHandlerJdbcService {
    public MysqlGaussTransferHandlerJdbcService(ISourceInfoService sourceInfoService, ISourceConfInfoService sourceConfInfoService, IDataTransferLogService dataTransferLogService, IDataTransferListService dataTransferListService, IDataTransferTaskService dataTransferTaskService, IDataTransferLogConverter dataTransferLogConverter, IDataTransferSqlStructureConverter dataTransferSqlStructureConverter, DataInfoSelectHandlerContext dataInfoSelectHandlerContext, IContrastTableStructureService contrastTableStructureService, SourceConfInfoRepository sourceConfInfoRepository) {
        super(sourceInfoService, sourceConfInfoService, dataTransferLogService, dataTransferListService, dataTransferTaskService, dataTransferLogConverter, dataTransferSqlStructureConverter, dataInfoSelectHandlerContext, contrastTableStructureService, sourceConfInfoRepository);
    }

    /**
     * @Method submitSourceData
     * @param targetConfInfoPoList
     * @param sourceData
     * @param targetTableName
     * @param delimiter
     * @param targetEncoding
     * @param resultMap
     * @Return void
     * @Description 提交源表中的数据
     * @Author Yin.Yang
     * @Date 2023/11/29 19:56
     * @Version V1.0
     */
    protected void submitSourceData(List<SourceConfInfoPo> targetConfInfoPoList, StringBuilder sourceData, String targetTableName, String delimiter, String targetEncoding, Map resultMap) throws SQLException, IOException {
        Connection jdbcConnection = JdbcUtil.getJdbcConnection(targetConfInfoPoList);
        BaseConnection baseConn = jdbcConnection.unwrap(BaseConnection.class);
        baseConn.setAutoCommit(false);
        String doTransferSql = "Copy " + targetTableName + " from STDIN DELIMITER " + "'" + delimiter + "'" + " ENCODING " + "'" + targetEncoding + "'";
        resultMap.put("doTransferSql", doTransferSql);
        CopyManager cp = new CopyManager(baseConn);
        StringReader reader = new StringReader(sourceData.toString());
        cp.copyIn(doTransferSql, reader);
        baseConn.commit();
        reader.close();
        baseConn.close();
    }
}
